<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="位置" prop="wz">
        <el-input
          v-model="queryParams.wz"
          placeholder="请输入位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="月份" prop="yf">
        <el-input
          v-model="queryParams.yf"
          placeholder="请输入月份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="年份" prop="nf">
        <!-- <el-input
          v-model="queryParams.nf"
          placeholder="请输入年份"
          clearable
          size="small"
        /> -->
        <el-date-picker
          v-model="queryParams.nf"
          type="year"
          placeholder="选择年份"
          value-format="yyyy"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker  :editable="false"
          clearable
          size="small"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button icon="el-icon-plus" size="mini" @click="generate"
          >生成</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Productionline_insect_main:insect_main:add']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Productionline_insect_main:insect_main:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Productionline_insect_main:insect_main:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Productionline_insect_main:insect_main:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="insect_mainList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="100px" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="月份" align="center" prop="yf" /> -->
      <el-table-column label="年份" align="center" prop="nf" />
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"

        min-width="250px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
          >导出</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline_insect_main:insect_main:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline_insect_main:insect_main:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改飞虫年度分析对话框 -->
    <el-dialog
      v-myFullscreen
      fullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      v-myDialogDrag
    >
      <TableHeader
        @nameChange="nameChange"
        :data="{
          mainTitle: '飞虫年度分析记录',
          company: '青岛崂山矿泉水有限公司',
          code: form.bbbh,
        }"
      />
      <el-divider content-position="center"></el-divider>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <!-- <el-col :span="12">
                <el-form-item label="位置" prop="wz">
                  <el-input v-model="form.wz" placeholder="请输入位置" :disabled="openDisabled" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="月份" prop="yf">
                  <el-input v-model="form.yf" placeholder="请输入月份" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->
          <el-col :span="12">
            <el-form-item label="年份" prop="nf">
              <!-- <el-input
                v-model="form.nf"
                placeholder="请输入年份"
                :disabled="openDisabled"
              /> -->
              <el-date-picker
                v-model="form.nf"
                type="year"
                placeholder="选择年份"
                value-format="yyyy"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
            
          </el-col>
          <el-col :span="12">
            <el-form-item label="分析说明" prop="fxsm">
               <el-input v-model="form.fxsm" placeholder="请输入分析说明" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="报表编号" prop="bbbh">
              <el-input
                v-model="form.bbbh"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-divider content-position="center">飞虫年度分析数据</el-divider>
        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddQualityProdInsectSub" :disabled="openDisabled">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQualityProdInsectSub" :disabled="openDisabled">删除</el-button>
          </el-col>
        </el-row> -->
        <el-table
          :data="qualityProdInsectSubList"
          :row-class-name="rowQualityProdInsectSubIndex"
          @selection-change="handleQualityProdInsectSubSelectionChange"
          ref="qualityProdInsectSub"
        >
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <!-- <el-table-column label="序号" align="center" prop="index" width="50"/> -->
          <el-table-column label="位置" prop="wz">
            <template slot-scope="scope">
              <!--  <el-input v-model="scope.row.wz" placeholder="请输入位置" :disabled="openDisabled"/> -->
              <el-select
                v-model="scope.row.wz"
                placeholder="请选择位置"
                style="width: 100px"
                :disabled="openDisabled"
              >
                <el-option
                  v-for="dict in productionline_InsectMain_wz"
                  :key="dict.dictSort"
                  :label="dict.dictLabel"
                  :value="dict.dictLabel"
                  :disabled="openDisabled"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="01月" prop="sl1">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl1"
                placeholder="请输入一月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="02月" prop="sl2">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl2"
                placeholder="请输入二月份字段"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="03月" prop="sl3">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl3"
                placeholder="请输入三月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="04月" prop="sl4">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl4"
                placeholder="请输入四月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="05月" prop="sl5">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl5"
                placeholder="请输入五月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="06月" prop="sl6">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl6"
                placeholder="请输入六月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="07月" prop="sl7">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl7"
                placeholder="请输入七月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="08月" prop="sl8">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl8"
                placeholder="请输入八月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="09月" prop="sl9">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl9"
                placeholder="请输入九月份数量"
                :disabled="openDisabled"
                @input="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="10月" prop="sl10">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl10"
                placeholder="请输入十月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="11月" prop="sl11">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl11"
                placeholder="请输入十一月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <el-table-column label="12月" prop="sl12">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl12"
                placeholder="请输入十二月份数量"
                :disabled="openDisabled"
                @blur="gbTrigger"
              />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
                <el-input v-model="scope.row.remark" placeholder="请输入备注" :disabled="openDisabled"/>
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <el-divider content-position="center"></el-divider>
      <el-row>
        <el-col :span="18">
          <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
          <div ref="main" style="width: 1200px; height: 800px"></div>
        </el-col>
        <!-- <el-col :span="6">
          <div label="分析说明">
            分析说明:&nbsp;&nbsp; <br />
            1.灭蝇设施存放位置正确，能及时捕捉飞虫。
          </div>
        </el-col> -->
      </el-row>

      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInsect_main,
  getInsect_main,
  delInsect_main,
  addInsect_main,
  updateInsect_main,
  getYearData,
} from "@/api/quality/productionline/insect_main";

export default {
  name: "Insect_main",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQualityProdInsectSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 飞虫年度分析表格数据
      insect_mainList: [],
      // 飞虫年度分析_子表格数据
      qualityProdInsectSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        wz: null,
        yf: null,
        nf: null,
        createTime: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      series: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("productionline_InsectMain_wz").then((response) => {
      this.productionline_InsectMain_wz = response.data;
    });
  },
  methods: {
    // 生成
    generate() {
      if (this.queryParams.nf == null) {
        this.msgWarning("请选择年份！");
      } else {
        getYearData({ nf: this.queryParams.nf }).then((res) => {
          let arr = res.data;
          this.reset();
          this.form.nf = this.queryParams.nf;
          // this.openDisabled = false;
          // this.open = true;
          arr.forEach((item) => {
            let obj = {};
            obj.wz = item.wz;
            obj.sl1 = item.sl1;
            obj.sl2 = item.sl2;
            obj.sl3 = item.sl3;
            obj.sl4 = item.sl4;
            obj.sl5 = item.sl5;
            obj.sl6 = item.sl6;
            obj.sl7 = item.sl7;
            obj.sl8 = item.sl8;
            obj.sl9 = item.sl9;
            obj.sl10 = item.sl10;
            obj.sl11 = item.sl11;
            obj.sl12 = item.sl12;
            obj.remark = "";
            this.qualityProdInsectSubList.push(obj);
          });
          this.form.qualityProdInsectSubList = this.qualityProdInsectSubList;
          addInsect_main(this.form)
              .then((response) => {
                this.msgSuccess("生成成功");
                this.getList();
              })
        });
      }
    },
    /** 查询飞虫年度分析列表 */
    getList() {
      this.loading = true;
      listInsect_main(this.queryParams).then((response) => {
        this.insect_mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 故障类型字典翻译
    productionline_InsectMain_wz_Format(row, column) {
      return this.selectDictLabel(this.productionline_InsectMain_wz, row.wz);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wz: null,
        yf: null,
        nf: null,
        bbbh: "LK-QF-LS1-10-015-11-05",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.qualityProdInsectSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.series = [];
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加飞虫年度分析";
      // 创建子表
      this.handleAddQualityProdInsectSub();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getInsect_main(id).then((response) => {
        this.form = response.data;
        this.qualityProdInsectSubList = response.data.qualityProdInsectSubList;
        this.open = true;
        this.title = "修改飞虫年度分析";
        // 折线图
        this.initChart(response);
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getInsect_main(id).then((response) => {
        this.form = response.data;
        this.qualityProdInsectSubList = response.data.qualityProdInsectSubList;
        this.open = true;
        this.title = "查看飞虫年度分析";
        // 折线图
        this.initChart(response);
      });
    },

    excelhandleView(row){
      this.download(
        "Productionline/insect_main/exportexcel?id="+row.id,
        {
          ...this.queryParams,
        },
        "飞虫年度分析" + this.parseTime(new Date()) + ".xlsx"
      );
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {

        if (valid) {
          this.form.qualityProdInsectSubList = this.qualityProdInsectSubList;
            /**打开遮罩层*/
            const rLoading = this.openLoading();
          if (this.form.id != null) {
            updateInsect_main(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          } else {
            addInsect_main(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除飞虫年度分析编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInsect_main(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 飞虫年度分析_子序号 */
    rowQualityProdInsectSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 飞虫年度分析_子添加按钮操作 */
    handleAddQualityProdInsectSub() {
      this.productionline_InsectMain_wz.forEach((item) => {
        let obj = {};
        obj.wz = item.dictLabel;
        obj.sl1 = "";
        obj.sl2 = "";
        obj.sl3 = "";
        obj.sl4 = "";
        obj.sl5 = "";
        obj.sl6 = "";
        obj.sl7 = "";
        obj.sl8 = "";
        obj.sl9 = "";
        obj.sl10 = "";
        obj.sl11 = "";
        obj.sl12 = "";
        obj.remark = "";
        this.qualityProdInsectSubList.push(obj);
      });
    },
    /** 飞虫年度分析_子删除按钮操作 */
    handleDeleteQualityProdInsectSub() {
      if (this.checkedQualityProdInsectSub.length == 0) {
        this.$alert("请先选择要删除的飞虫年度分析_子数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.qualityProdInsectSubList.splice(
          this.checkedQualityProdInsectSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleQualityProdInsectSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.qualityProdInsectSub.clearSelection();
        this.$refs.qualityProdInsectSub.toggleRowSelection(selection.pop());
      } else {
        this.checkedQualityProdInsectSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "Productionline_insect_main/insect_main/export",
        {
          ...this.queryParams,
        },
        "飞虫年度分析" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },
    /* 输入框改变时触发 */
    gbTrigger() {
      this.form.qualityProdInsectSubList = this.qualityProdInsectSubList;
      if (this.form.id != null) {
        updateInsect_main(this.form).then((response) => {
          getInsect_main(this.qualityProdInsectSubList[0].parentid).then(
            (response) => {
              // 折线图
              this.initChart(response);
            }
          );
        });
      }
    },
    /* 初始化echarts实例  */
    initChart(res) {
      this.$nextTick(() => {
        //基于准备好的dom，初始化echarts实例
        let myChart = this.$echarts.init(this.$refs.main);

        //中间对option数据进行处理
        let option = {
          backgroundColor: "#407EB4",
          title: {
            top: "3%",
            left: "45%",
            text: "虫害趋势图",
            textStyle: {
              color: "#ffffff", //字体颜色
            },
          },
          // tooltip: {
          //   trigger: "axis",
          // },
          legend: {
            top: "93%",
            data: [],
            type: "scroll", //设置legend滚动轴的出现
            textStyle: {
              color: "#ffffff", //字体颜色
            },
          },
          grid: {
            left: "3%",
            right: "4%",
            bottom: "10%",
            containLabel: true,
          },
          toolbox: {
            feature: {
              saveAsImage: {},
            },
          },
          xAxis: {
            type: "category",
            boundaryGap: false,
            data: [
              "01月",
              "02月",
              "03月",
              "04月",
              "05月",
              "06月",
              "07月",
              "08月",
              "09月",
              "10月",
              "11月",
              "12月",
            ],
            axisLabel: {
              show: true,
              textStyle: {
                color: "#ffffff",
              },
            },
          },
          yAxis: {
            type: "value",
            axisLabel: {
              show: true,
              textStyle: {
                color: "#ffffff",
              },
            },
          },
          series: this.series,
        };
        /* 遍历位置 */
        this.productionline_InsectMain_wz.forEach((item) => {
          option.legend.data.push(item.dictLabel);
        });
        /* 组装数据 */
        res.data.qualityProdInsectSubList.forEach((item) => {
          let obj = {};
          obj.name = item.wz;
          obj.type = "line";
          const nclist = [];
          nclist.push(item.sl1);
          nclist.push(item.sl2);
          nclist.push(item.sl3);
          nclist.push(item.sl4);
          nclist.push(item.sl5);
          nclist.push(item.sl6);
          nclist.push(item.sl7);
          nclist.push(item.sl8);
          nclist.push(item.sl9);
          nclist.push(item.sl10);
          nclist.push(item.sl11);
          nclist.push(item.sl12);
          obj.data = nclist;
          this.series.push(obj);
        });
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
      });
    },
  },
};
</script>
